package com.shxzz.easy12306.framework.starter.user.core;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * 用户信息上下文
 * @author shxzz
 * @create 2024-02-24-18:54
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class UserContext {
    private static final ThreadLocal<UserInfoDTO> USER_INFO_CONTEXT = new ThreadLocal<>();

    public static UserInfoDTO getUser() {
        return USER_INFO_CONTEXT.get();
    }

    public static String getUserId() {
        return getUser() == null ? null : getUser().getUserId();
    }

    public static String getUsername() {
        return getUser() == null ? null : getUser().getUsername();
    }

    public static void setUser(UserInfoDTO userInfo) {
        USER_INFO_CONTEXT.set(userInfo);
    }

    public static void removeUser() {
        USER_INFO_CONTEXT.remove();
    }
}
